<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\common\model\Newscat;
use think\Db;

/**
 * 前台首页控制器
 * @package app\index\controller
 */
class Message extends Auth
{
    public $noNeedLogin = [];

    /**
     * 获取公告列表
     */
    public function notice()
    {
        if ($this->request->isGet()) {
            $getData = $this->request->get();
            if (empty($getData['page'])) {
                $getData['page'] = 1;
            }
            $pageData = $this->request->get('limit');
            if(empty($pageData) || !isset($pageData)){
                $limit = 10;
            }else{
                $limit = $pageData;
            }
            $noticData = Db::name('qt_notic')
                ->field('id,title,img,desc,created_time')
                ->where('type','2')
                ->order('id desc')
                ->paginate($limit)->each(function ($item){
                    $item['img'] = get_img_path($item['img']);
                    return $item;
                });
            $this->result('查询成功', $noticData, 200);
        }
        $this->result('请求参数有误', '', 204);


    }

    /**
     * 工作通知
     */
    public function lists(){
        $getData = $this->request->post();
        if (empty($getData['page'])) {
            $getData['page'] = 1;
        }
        $pageData = $this->request->post('limit');
        if(empty($pageData) || !isset($pageData)){
            $limit = 10;
        }else{
            $limit = $pageData;
        }
        $userInfo = $this->userInfo;
        $sql = Db::name('qt_message')
            ->field('id,title,content,type,read,order_id,created_time')
            ->whereTime('created_time',$getData['time'])
            ->where(['to_user_id'=>$userInfo['id']])
            ->order('id desc');
        if(isset($getData['read']) && $getData['read'] != ""){
            $sql->where('read',$getData['read']);
        }
        if(isset($getData['room_id']) && !empty($getData['room_id'])){
            $sql->where('room_id',$getData['room_id']);
        }
        if(isset($getData['type']) && !empty($getData['type'])){
            $sql->where('type',$getData['type']);
        }
        if(isset($getData['time'])){
            $noticData = $sql->whereTime('created_time',$getData['time'])->paginate($limit);
        }else{
            $noticData = $sql->paginate($limit);
        }
        $this->result('查询成功', $noticData, 200);
    }


    /**
     * 获取公告详情
     */

    public function detail()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['order_id'])) {
                $this->result('请求参数有误', '', 204);
            }
            if (empty($getData['message_id'])) {
                $this->result('请求参数有误', '', 204);
            }
            $noticData = Db::name('qt_notic')
                ->field('id,title,img,desc,content,created_time')
                ->where(['id'=>$getData['order_id']])
                ->find();
            $noticData['img'] = get_img_path($noticData['img']);
            Db::name('qt_message')->where('id',$getData['message_id'])->update(['read'=>'1']);
            $this->result('查询成功', $noticData, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 获取用户工作状态
     */
    public function work_status(){
        if ($this->request->isGet()) {
            $userInfo = $this->userInfo;
            $this->result('查询成功',$userInfo['work_status'],200);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 获取用户工作状态
     */
    public function save_status(){
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $status = $this->request->post('work_status');
            if(empty($status)){
                $this->result('缺少参数', '', 204);
            }
            $save = Db::name('qt_users')->where('id',$userInfo['id'])->update(['work_status'=>$status]);
            if($save !== false){
                $this->result('修改成功',$status,200);
            }
            $this->result('修改失败','',204);
        }
        $this->result('无效的请求', '', 204);
    }


}
